package utils;

import com.kennycason.kumo.CollisionMode;
import com.kennycason.kumo.WordCloud;
import com.kennycason.kumo.WordFrequency;
import com.kennycason.kumo.bg.CircleBackground;
import com.kennycason.kumo.font.KumoFont;
import com.kennycason.kumo.font.scale.SqrtFontScalar;
import com.kennycason.kumo.palette.LinearGradientColorPalette;

import java.awt.*;
import java.util.List;

public class WordCloudUtil {

    /**
     * 根据集合生成圆形词云
     * @param wordFrequencies WordFrequency 的集合对象
     *                        WordFrequency(单词，单词数量)
     */
    public static void createWordCloud(List<WordFrequency> wordFrequencies){

        Dimension dimension = new Dimension(300, 300);

        //设置图片相关的属性,这边是大小和形状,更多的形状属性,可以从CollisionMode源码里面查找
        WordCloud wordCloud = new WordCloud(dimension, CollisionMode.PIXEL_PERFECT);
        wordCloud.setPadding(2);

        //这边要注意意思,是设置中文字体的,如果不设置,得到的将会是乱码,
        java.awt.Font font = new java.awt.Font("STSong-Light", 2, 40);
        wordCloud.setKumoFont(new KumoFont(font));
        // 设置背景色
        wordCloud.setBackgroundColor(new Color(255, 255, 255));
        // 设置圆的半径
        wordCloud.setBackground(new CircleBackground(150));
        //设置颜色
        wordCloud.setColorPalette(new LinearGradientColorPalette(Color.RED, Color.BLUE, Color.GREEN, 30, 30));
        wordCloud.setFontScalar(new SqrtFontScalar(12, 45));
        // 自定义横纵比例
        wordCloud.setAngleGenerator(new MyAngleGenerator());

        //将文字写入图片
        wordCloud.build(wordFrequencies);
        //生成图片
        wordCloud.writeToFile("output/chinese_language_circle.png");
    }

}
